package com.fengze.business.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.fengze.business.domain.FileInfo;
import com.fengze.business.enums.BusinessTypeEnums;
import com.fengze.business.mapper.FileInfoMapper;
import com.fengze.common.annotation.AutoSetInsertValue;
import com.fengze.common.annotation.AutoSetUpdateValue;
import com.fengze.common.config.RuoYiConfig;
import com.fengze.common.utils.DateUtils;
import com.fengze.common.utils.StringUtils;
import com.fengze.common.utils.file.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fengze.business.mapper.InvoiceInfoMapper;
import com.fengze.business.domain.InvoiceInfo;
import com.fengze.business.service.IInvoiceInfoService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * 发票信息Service业务层处理
 * 
 * @author lug
 * @date 2024-02-28
 */
@Service
public class InvoiceInfoServiceImpl implements IInvoiceInfoService 
{
    @Autowired
    private InvoiceInfoMapper invoiceInfoMapper;

    @Autowired
    private FileInfoMapper fileInfoMapper;

    /**
     * 查询发票信息
     * 
     * @param id 发票信息主键
     * @return 发票信息
     */
    @Override
    public InvoiceInfo selectInvoiceInfoById(Long id)
    {

        InvoiceInfo invoiceInfo = invoiceInfoMapper.selectInvoiceInfoById(id);
        List<FileInfo> fileInfos = fileInfoMapper.selectListByBusiIdAndType(id,BusinessTypeEnums.INVOICE.getCode());
        invoiceInfo.setFileInfoList(fileInfos);
        return invoiceInfo;
    }

    /**
     * 查询发票信息列表
     * 
     * @param invoiceInfo 发票信息
     * @return 发票信息
     */
    @Override
    public List<InvoiceInfo> selectInvoiceInfoList(InvoiceInfo invoiceInfo)
    {
        return invoiceInfoMapper.selectInvoiceInfoList(invoiceInfo);
    }

    /**
     * 新增发票信息
     * 
     * @param invoiceInfo 发票信息
     * @return 结果
     */
    @Override
    public int insertInvoiceInfo(InvoiceInfo invoiceInfo)
    {
        invoiceInfo.setCreateTime(DateUtils.getNowDate());
        return invoiceInfoMapper.insertInvoiceInfo(invoiceInfo);
    }

    /**
     * 修改发票信息
     * 
     * @param invoiceInfo 发票信息
     * @return 结果
     */
    @Override
    public int updateInvoiceInfo(InvoiceInfo invoiceInfo)
    {
        invoiceInfo.setUpdateTime(DateUtils.getNowDate());
        return invoiceInfoMapper.updateInvoiceInfo(invoiceInfo);
    }

    /**
     * 批量删除发票信息
     * 
     * @param ids 需要删除的发票信息主键
     * @return 结果
     */
    @Override
    public int deleteInvoiceInfoByIds(Long[] ids)
    {
        return invoiceInfoMapper.deleteInvoiceInfoByIds(ids);
    }

    /**
     * 删除发票信息信息
     * 
     * @param id 发票信息主键
     * @return 结果
     */
    @Override
    public int deleteInvoiceInfoById(Long id)
    {
        return invoiceInfoMapper.deleteInvoiceInfoById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @AutoSetInsertValue
    @Override
    public int insertInvoiceInfoWithFiles(InvoiceInfo invoiceInfo, MultipartFile[] files) {
        invoiceInfo.setStatus(0);
        int count = invoiceInfoMapper.insertInvoiceInfo(invoiceInfo);
        if (null != files && files.length > 0) {
            for (MultipartFile file : files) {
                // 上传文件路径
                String filePath = RuoYiConfig.getUploadPath();
                // 上传并返回新文件名称
                String fileName = null;
                try {
                    fileName = FileUploadUtils.upload(filePath, file);
                    FileInfo fileInfo = new FileInfo();
                    fileInfo.setStatus(0);
                    fileInfo.setBusiId(invoiceInfo.getId());
                    fileInfo.setBusiType(BusinessTypeEnums.INVOICE.getCode());
                    fileInfo.setFileName(file.getOriginalFilename());
                    fileInfo.setFileType(FileUploadUtils.getExtension(file));
                    fileInfo.setFileSize(file.getSize());
                    fileInfo.setFileUrl(fileName);
                    fileInfoMapper.insertFileInfo(fileInfo);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return count;
    }

    @Transactional(rollbackFor = Exception.class)
    @AutoSetUpdateValue
    @Override
    public int updateInvoiceInfoWithFiles(InvoiceInfo invoiceInfo, MultipartFile[] files) {

        // 逻辑删除文件信息
        String deleteFileInfoIdStr = invoiceInfo.getDeleteFileInfoIdStr();
        if (StringUtils.isNotEmpty(deleteFileInfoIdStr)) {
            fileInfoMapper.deleteLogicalByBusiIdAndType(invoiceInfo.getId(),BusinessTypeEnums.INVOICE.getCode(), 1);
        }


        if (null != files && files.length > 0) {
            for (MultipartFile file : files) {
                // 上传文件路径
                String filePath = RuoYiConfig.getUploadPath();
                // 上传并返回新文件名称
                String fileName = null;
                try {
                    fileName = FileUploadUtils.upload(filePath, file);
                    FileInfo fileInfo = new FileInfo();
                    fileInfo.setStatus(0);
                    fileInfo.setBusiId(invoiceInfo.getId());
                    fileInfo.setBusiType(BusinessTypeEnums.INVOICE.getCode());
                    fileInfo.setFileName(file.getOriginalFilename());
                    fileInfo.setFileType(FileUploadUtils.getExtension(file));
                    fileInfo.setFileSize(file.getSize());
                    fileInfo.setFileUrl(fileName);
                    fileInfoMapper.insertFileInfo(fileInfo);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return invoiceInfoMapper.updateInvoiceInfo(invoiceInfo);
    }

    @Override
    public int examineInfo(Long id, Long userId, Date auditTime,Integer status) {
        return invoiceInfoMapper.audit(id,userId,auditTime,status);
    }

    @Override
    public int open(InvoiceInfo invoiceInfo) {
        return invoiceInfoMapper.open(invoiceInfo.getId(),invoiceInfo.getInvoiceNumber(),invoiceInfo.getInvoiceTime(),invoiceInfo.getInvoiceUserId(),2);
    }
}
